﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class SystemManage_PlaceAdd : BasePage
{
    ISyPlaceHandler placeManager = new SyPlaceHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!strPermission.Contains("1012"))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }

        }
    }

    private DataTable NameExists(string districtCN, string districtHK, string districtEN, string nameCN, string nameHK, string nameEN)
    {

        SqlParameter[] prams = new SqlParameter[6];
        prams[0] = new SqlParameter("@District", districtCN);
        prams[1] = new SqlParameter("@Name", nameCN);
        prams[2] = new SqlParameter("@HKName", nameHK);
        prams[3] = new SqlParameter("@ENGName", nameEN);
        prams[4] = new SqlParameter("@HKDistrict", districtHK);
        prams[5] = new SqlParameter("@ENGDistrict", districtEN);
        string sql = "select * from Sy_Place where (District = @District or HKDistrict = @HKDistrict or  ENGDistrict = @ENGDistrict ) and (Name = @Name or HKName = @HKName or ENGName = @ENGName)";
        DataTable dt = placeManager.GetList(sql, prams);

        return dt;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {


        DataTable dt = NameExists(txtDistrict.Text.Trim(), txtHKDistrict.Text.Trim(), txtENDistrict.Text.Trim(), txtName.Text.Trim(), txtHKName.Text.Trim(), txtENName.Text.Trim());
        if (dt != null && dt.Rows.Count > 0)
        {
            Jscript.Alert(Resources.Messages.NameExists, this.Page);
            return;
        }
        else
        {
            Sy_PlaceQuery placeQuery = new Sy_PlaceQuery();
            placeQuery.Code = string.Empty;
            placeQuery.Name = txtName.Text.Trim();
            placeQuery.HKName = txtHKName.Text.Trim();
            placeQuery.ENGName = txtENName.Text.Trim();
            placeQuery.District = txtDistrict.Text.Trim();
            placeQuery.HKDistrict = txtHKDistrict.Text.Trim();
            placeQuery.ENGDistrict = txtENDistrict.Text.Trim();
            placeQuery.Remark = txtRemark.Text;
            if (radYes.Checked)
            {
                placeQuery.Status = 1;
            }
            else
            {
                placeQuery.Status = 0;
            }
            placeQuery.LastModDate = DateTime.Now;
            placeQuery.LastModUser = int.Parse(Session["UserID"].ToString());

            try
            {
                if (placeManager.AddPlace(placeQuery))
                {
                    Jscript.AlertAndRedirect(Resources.Messages.AddSuccess, "PlaceList.aspx", this.Page);
                }
                else
                {
                    Jscript.Alert(Resources.Messages.AddFailed, this.Page);
                }
            }
            catch
            {
                Jscript.Alert(Resources.Messages.AddFailed, this.Page);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("PlaceList.aspx");
    }
}
